package com.barryredmond.javamelibchecker;

import javax.microedition.lcdui.ChoiceGroup;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.Item;
import javax.microedition.lcdui.ItemStateListener;
import javax.microedition.lcdui.StringItem;
import javax.microedition.lcdui.TextField;

public  class UserInputClassChecker extends Form implements CommandListener, ItemStateListener {
	
	private static final String NOT_CHECKED_YET = "Not Checked Yet";
	
	private final Command back; 
	private final Command check;
	
	private Displayable previous;
	
	private StringItem instructions;
	private TextField inputBox;
	private StringItem isAvailable;
	
	private boolean isAvailableSet = false;
	
	public UserInputClassChecker( Displayable previous ) {
		super( "Check Class Exists" );
		this.previous = previous;
		
		this.addCommand( back = new Command( "Back", Command.BACK, 0));
		this.addCommand( check = new Command( "Check", Command.OK, 1 ));
		this.setCommandListener(this);
		
		instructions = new StringItem("Instructions", "Type in the full class name of the class you wish to check for.(e.g. javax.microedition.lcdui.Form)");
		this.append(instructions);
		inputBox = new TextField( "Class to Check for: ", "", 500, TextField.ANY );
		this.setItemStateListener(this);
		this.append(inputBox);
		isAvailable = new StringItem( "Is Available?", NOT_CHECKED_YET );
		this.append( isAvailable );
	}

	public void commandAction(Command command, Displayable disp) {
		if( command==back ){
			Display.getDisplay(LibCheckerMIDlet.getMIDlet()).setCurrent(previous);
		}else if( command==check ){
			check();
		}
	}

	public void itemStateChanged(Item item) {
		if( item==inputBox ){
			check();
		}
	}
	
	private void check() {
		SupportableJSR sup = new SupportableJSR(inputBox.getString(), inputBox.getString());
		if( sup.isSupported.booleanValue() ){
			isAvailable.setText("Yes");
		}else{
			isAvailable.setText("No");
		}
		isAvailableSet = true;
	}
}
